﻿var week = Array(
	Array("Sunday", "周日"),
	Array("Monday", "周一"),
	Array("Tuesday", "周二"),
	Array("Wednesday", "周三"),
	Array("Thursday", "周四"),
	Array("Friday", "周五"),
	Array("Saturday", "周六")
);

$(document).on('pageshow', '#default', function(){
	//跳转当前星期
	var nowPage = week[new Date().getDay()][0];
	$.mobile.changePage("#" + nowPage, {transition: "slide"});
});

$(document).on('pagehide', function(data1,data2){
	console.log(data2.nextPage[0].attributes.id.value);
	$('.bangumiContent').html('');
	var weekday = 'none'
	for(var i=0; i<=6; i++){
		if(week[i][0] == data2.nextPage[0].attributes.id.value)
			weekday = String(i);
	}
	var ch = $("[name='ch']:checked").val();
	console.log(ch);
	if('none' != weekday){
		//console.log('b');
		showLoader();
		$.ajax({
			url: './php/get_bangumi.php?weekday='+weekday+'&ch='+ch,
			dataType: 'json',
			success:
				function(data){
					console.log(data);
					hideLoader();
					if(data.error){
						$('#warningText').html(data.error);
						$('#warning').click();
						//console.log('a');
					}else{
						for(var i in data.list){
							if(data.list[i].weekday == weekday){
								//console.log("[id='"+week[weekday][0]+"List']");
								var time = moment.unix(parseInt(data.list[i].lastupdate));
								var str = '<li data-role="list-divider">'+time.format('MM月DD日 HH:mm')+'</li><li><a id="spBtn" href="./spContent.php?spid='+data.list[i].spid+'" data-prefetch="true"><img src="'+data.list[i].cover+'"><h2>'+data.list[i].title+'</h2><p>点击量：'+data.list[i].click+'</p>';
								if(data.list[i].new){
									str += '<span class="ui-li-count">new</span>';
								}
								str += '</a></li>'
								$("[id='"+week[weekday][0]+"List']").append(str);
							}
						}
						$("[id='"+week[weekday][0]+"List']").listview('refresh');
					}
				}
		});
	}
});